{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0bd32972-13e8-475c-9415-c50a50409568",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e35e35f2-89e4-4243-8c6c-bf3abe8b422c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!cd modelscope-agent && pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ff60b4f",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d194e5af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/agents')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1f21a9c",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d69cdbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n",
    "print('请输入ModelScope Token')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a02062",
   "metadata": {},
   "source": [
    "### 构建多工具agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f4e99b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.agents import RolePlay\n",
    "\n",
    "role_template = '你一个人类助手，可以利用工具帮助人类做许多事情'\n",
    "\n",
    "llm_config = {\n",
    "    'model': 'qwen-max', \n",
    "    'model_server': 'dashscope',\n",
    "    }\n",
    "function_list = ['speech-generation','text-address']\n",
    "bot = RolePlay(function_list=function_list,llm=llm_config, instruction=role_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1f65122",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95597ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run(\"写一个20字左右简短的小故事并朗读\", remote=False, print_info=True)\n",
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db252e62",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "from IPython.display import Audio\n",
    "\n",
    "# 使用正则表达式提取音频文件路径\n",
    "match = re.search(r'src=\"([^\"]+)\"', text)\n",
    "if match:\n",
    "    audio_file_path = match.group(1)\n",
    "    audio = Audio(audio_file_path)\n",
    "    display(audio)\n",
    "else:\n",
    "    print(\"No audio file found in the observation string.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a744de",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run(\"使用地址识别模型，从下面的地址中找到省市区等元素，地址：浙江杭州市江干区九堡镇三村村一区\", remote=False, print_info=True)\n",
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
